import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import {
  getAddressListData,
  getCategoryListData,
  getProductListData
} from "../../service";

export const fetchHomeDataAction = createAsyncThunk("fecthdata", (playLoad, { dispatch }) => {

  getAddressListData().then(res => {
    dispatch(changeAddressListAction(res.data))
  })

  getCategoryListData().then(res => {
    dispatch(changeCategoryListAction(res.data))
  })

  getProductListData({}).then(res => {
    dispatch(changeProductListAction(res.data))
  })

})


const homeSlice = createSlice({
  name: "home",
  initialState: {
    addressList: undefined,
    categoryList: undefined,
    productList: undefined,
    productListTotal: 0
  },
  reducers: {
    changeAddressListAction(state, { payload }) {
      state.addressList = payload
    },
    changeCategoryListAction(state, { payload }) {
      state.categoryList = payload
    },
    changeProductListAction(state, { payload }) {
      state.productList = payload.list
      state.productListTotal = payload.total
    },

  }

});

export const {
  changeAddressListAction,
  changeCategoryListAction,
  changeProductListAction
} = homeSlice.actions

export default homeSlice.reducer;
